//
//  WHWelcomeViewController.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/9.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import AFNetworking
import SDWebImage

class WHWelcomeViewController: UIViewController {

    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.steupUI()
    }
    
    
    //移动
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
        
    }
    
    
    private func startAnimation(){
        //计算off
        let offY = -ScreenHeight + 180
        
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(offY)
        }
        
        nameLabel.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //名字动画
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    
                        self.nameLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //登录成功
                        NSNotificationCenter.defaultCenter().postNotificationName(WHSwitchRootViewController, object: "main")
                })
        }
        
    }
    
    
    private func steupUI(){
        view.addSubview(iconImageView)
        view.addSubview(nameLabel)
        
        //约束
                iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
        }
        
        iconImageView.cornerRadius = 30

        //加载头像
//        iconImageView.sd_setImageWithURL(WHUserAccountViewModel.sharedAccountViewModel.headUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        iconImageView.sd_setImageWithURL(WHUserAccountViewModel.sharedAccountViewModel.headUrl)
        
    }
    
    //懒加载
    lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    lazy var nameLabel: UILabel = {
        let nameL = UILabel()
        nameL.text = WHUserAccountViewModel.sharedAccountViewModel.accountModel?.name
        nameL.textColor = UIColor.darkGrayColor()
        nameL.font = UIFont.systemFontOfSize(15)
        nameL.sizeToFit()
        return nameL
    }()
}
